package com.miaoxie.forum.module.monitor.shared.util;

import com.miaoxie.forum.module.monitor.shared.entity.ZLjobEntity;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;

/**
 * Created by miaoxie on 2017/3/30.
 */
public class DebugRunnable implements Callable {
    String url;
    String parm;
    public DebugRunnable(String url,String parm){
        this.url = url;
        this.parm = parm;
    }
    public List<ZLjobEntity> jobList(){
        WebDriver webDriver = SeleniumUtil.requestUrl();
        webDriver.get(url+"?"+parm);
        List<WebElement> tableWb = webDriver.findElements(By.xpath("//*[@id=\"newlist_list_content_table\"]/table/tbody"));
        List<ZLjobEntity> zLjobEntities = new ArrayList<>();
        tableWb.remove(0);
        for (WebElement w : tableWb){
            ZLjobEntity zl = new ZLjobEntity();
            List<WebElement> webe = w.findElements(By.xpath("tr/td[1]/div/a"));//获取职位名称
            String job = webe.get(0).getAttribute("innerText");
            zl.setJob(job);
            String jobHref = webe.get(0).getAttribute("href");
            zl.setJobHref(jobHref);
            webe = w.findElements(By.xpath("tr/td[2]/span"));//获取反馈率
            String fk_lv = webe.get(0).getAttribute("innerHTML");
            zl.setFk_lv(fk_lv);
            webe = w.findElements(By.xpath("tr/td[3]/a"));//获取公司信息
            String gsmc =webe.get(0).getAttribute("innerText");
            String gsmcHref = webe.get(0).getAttribute("href");
            zl.setGsmc(gsmc);
            zl.setGsmcHref(gsmcHref);
            webe = w.findElements(By.xpath("tr/td[4]"));//获取薪资信息
            String zwyx = webe.get(0).getAttribute("innerText");
            zl.setZwyx(zwyx);
            webe = w.findElements(By.xpath("tr/td[5]"));//获取工作地点
            String gzdd =webe.get(0).getAttribute("innerText");
            zl.setGzdd(gzdd);
            webe = w.findElements(By.xpath("tr/td[6]/span"));//获取发布时间
            String gxsj =webe.get(0).getAttribute("innerText");
            zl.setGxsj(gxsj);
            zLjobEntities.add(zl);
        }
        webDriver.close();
        webDriver.quit();

        return zLjobEntities;
    }
    public List<ZLjobEntity> jobList1() throws IOException {

        Document du = Jsoup.connect(url+"?"+parm).get();
        Elements elements = du.select("table.newlist");
        if(elements.size()==0)
            return null;
        elements.remove(0);
        System.out.println(du.title());
        List<ZLjobEntity> zLjobEntities = new ArrayList<>();
        for(Element e : elements){
            ZLjobEntity zl = new ZLjobEntity();
            Element e1 = e.select("tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(1) > div:nth-child(2) > a:nth-child(1)").first();
            String job = e1.text();
            String jobHref = e1.attr("href");
            zl.setJob(job);
            zl.setJobHref(jobHref);
            e1 = e.select("tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(2) > span:nth-child(1)").first();
            String fk_lv = e1.text();
            zl.setFk_lv(fk_lv);
            e1 = e.select("tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(3) > a:nth-child(1)").first();
            String gsmc = e1.text();
            String gsmcHref = e.attr("href");
            zl.setGsmc(gsmc);
            zl.setGsmcHref(gsmcHref);
            e1 = e.select("tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(4)").first();
            String zwyx = e1.text();
            zl.setZwyx(zwyx);
            e1 = e.select("tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(5)").first();
            String gzdd = e1.text();
            zl.setGzdd(gzdd);
            e1 = e.select("tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(6) > span:nth-child(1)").first();
            String gxsj = e1.text();
            zl.setGxsj(gxsj);

            zLjobEntities.add(zl);
        }

        return zLjobEntities;
    }
    /**
     * Computes a result, or throws an exception if unable to do so.
     *
     * @return computed result
     * @throws Exception if unable to compute a result
     */
    @Override
    public List<ZLjobEntity> call() throws Exception {
        System.out.println("现在是第"+parm+"线程名"+Thread.currentThread().getName());
        return jobList1();
    }
}
